package com.wzcl.app.service.common.activity;

import com.alibaba.fastjson.JSON;
import com.wzcl.app.dao.model.Activity;
import com.wzcl.app.dao.model.common.Promotion;
import com.wzcl.app.model.common.BusinessException;
import com.wzcl.app.model.common.CodeMsg;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.WZCL;
import com.wzcl.app.model.dto.PromotionDto;
import com.wzcl.app.service.ActivityService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Data
public class ActivityPromotionFactory<T extends Promotion> {

    private ActivityService activityService;

    private ActivityPromotion<T> promotionService;

    public ActivityPromotionFactory(ActivityService activityService, ActivityPromotion<T> promotionService) {
        this.activityService = activityService;
        this.promotionService = promotionService;
    }

    //为活动添加一个优惠
    public T addPromotion(PromotionDto promotionDto) {
        //校验参数合法性
        promotionService.checkValid(promotionDto);
        //校验活动存在性
        Activity activity = activityService.getAndCheckById(promotionDto.getActivityId());
        //校验活动和类型的匹配
        if (!activity.getType().equals(promotionDto.getType())) {
            throw new BusinessException(CodeMsg.ACTIVITY_TYPE_ERROR, "创建的活动优惠类型不匹配");
        }
        //冲突校验(例如:秒杀活动只能有一个)
        promotionService.checkConflict(promotionService.findAllByActivityId(activity.getId()));
        //创建添加
        return promotionService.create(promotionDto);
    }

    public T updatePromotion(PromotionDto promotionDto) {
        //校验参数合法性
        promotionService.checkValid(promotionDto);
        //修改
        return promotionService.update(promotionDto);
    }

    public T deletePromotion(Integer promotionId) {
        //删除
        return promotionService.deleteById(promotionId);
    }

    public T getPromotionById(Integer promotionId) {
        //获取
        return promotionService.getById(promotionId);
    }

    public void deletePromotionByActivityId(Integer activityId) {
        //删除
        promotionService.deleteAllByActivityId(activityId);
    }

    public List<T> findAllPromotion(Integer activityId) {
        return promotionService.findAllByActivityId(activityId);
    }

    public MyPage<T> findPromotionPage(PromotionDto promotionDto) {
        return promotionService.findPage(promotionDto);
    }

    public BigDecimal computePrice(Activity activity, BigDecimal price, String orderFrom) {
        if (StringUtils.startsWith(orderFrom, WZCL.ORDER_TYPE.SECKILL)) {
            if (WZCL.ACTIVITY_TYPE.SECKILL.equals(activity.getType())) {
                return computePrice(activity.getId(), price, null);
            }
        } else if(StringUtils.startsWith(orderFrom, WZCL.ORDER_TYPE.GROUP)){
            if (WZCL.ACTIVITY_TYPE.GROUP.equals(activity.getType())) {
                String conditionStr = orderFrom.substring(orderFrom.indexOf(":")+1);
                Map<String, Object> condition = (Map)JSON.parse(conditionStr);
                return computePrice(activity.getId(), price, condition);
            }
        }else{
            if (WZCL.ACTIVITY_TYPE.SINGLE.equals(activity.getType()) || WZCL.ACTIVITY_TYPE.FULL_CUT.equals(activity.getType())) {
                return computePrice(activity.getId(), price, null);
            }
        }
        return price;
    }

    //计算活动的价格
    private BigDecimal computePrice(Integer activityId, BigDecimal price, Map<String, Object> condition) {
        return promotionService.computePrice(activityId, price, condition);
    }

}
